'use strict'

const util = require('util')
const path = require('path')

const koa         = require('koa')
const views       = require('koa-views')
const logger      = require('koa-logger')
const Router      = require('koa-router')
const bodyParser = require('koa-bodyparser')
const router      = new Router()

const app = koa()

const UserController = require('./controllers/user')

router
  .get('/',function *(next){
    yield this.render('index',{title:'首页'})
  })
  .get('/wxLogin', UserController.wechatLogin)
  .post('/wxLogin',UserController.bindAccount)
  .get('/callback',UserController.wechatCallback)

// 注意中间件的顺序，先要写模板引擎，后写路由
app
  .use(logger())
  .use(views(path.join(__dirname,'views','pages'), {
      extension: 'jade'
  }))
  .use(bodyParser())
  .use(router.routes())
  .listen(8080, ()=> {
      util.log('server listening at port 8080')
  })
